package web

import (
	"gitee.com/crack007/goose/gslog"
	"github.com/gin-gonic/gin"
)

// 响应参数日志中间件，需要放到其他的中间件之后调用。
var ResponseLogMiddleware = &responseLogMiddleware{}

type responseLogMiddleware struct {
}

func (x *responseLogMiddleware) Apply(iRoutes gin.IRoutes) {
	iRoutes.Use(x.Handle)
}

func (x *responseLogMiddleware) Handle(ctx *gin.Context) {
	ctx.Next()
	// 获取捕获的响应数据
	writer, ok := ctx.Writer.(*responseWriter)
	if !ok {
		gslog.Error("responseLogMiddleware: failed to capture response data")
		return
	}

	// 打印响应日志
	gslog.Info("%s [response]: %s", ctx.Request.URL.Path, string(writer.body))
}
